<?php

class controller_product extends __registry_controller {

    public $_ = array();
    public $_layout = NULL;
    public $_content_header = 'OPEN';
    public $_content_top = 'OPEN';

    public function __construct() {
        parent::add_object(array(
            'handle_string',
            'handle_upload',
            'handle_paging',
            'model_product',
            'handle_validate',
            'handle_permission',
            'model_users',
            'model_categories',
            'model_language'
        ));
        $this->_layout = NULL;
    }

    public function index_action($key = 'all', $catid = 0) {
        /* Check permission */
        $this->per_check_permission();
        $limit = 15;
        $res_html = NULL;
        $sel_html = NULL;
        $seo2 = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
        $enseo = end($this->_seokey);
        if (empty($seo2) || ($seo2 == $enseo && intval($enseo) != 0)) {
            $this->_['url_add'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('add') . '/';
            $this->_['url_edit'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('edit') . '/';
        } else {

            $this->_['url_add'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('add') . '/' . $seo2 . '/';
            $this->_['url_edit'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('edit') . '/' . $seo2 . '/';
        }
        $seo2 = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
        $row = $this->mdc_get_cat_cid($seo2);

        /* Set lai data cho key vaf catid khi search */
                 
        $current = end($this->_seokey) ? intval(end($this->_seokey)) : 1;
        /*set lai data khi search*/
        if (count($this->_seokey) > 5) {
            $key = $this->_seokey[4];
            $catid = $this->_seokey[5];
            $current = 1;
        }
        /* set lai data khi click chuyen trang */
        if (count($this->_seokey) == 5) {
            $key = $this->_seokey[2];
            $catid = $this->_seokey[3];
        }
        $url = !empty($cid) ? URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1] . '/' . $key . '/' . $catid . '/' . $seo2 : URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1] . '/' . $key . '/' . $catid;
        $this->_['key'] = $key == 'all' ? $key = '' : $key;
        $data_sel_box = $this->mdp_get_dataselectbox($this->_seokey[1], $catid);

        $cid = isset($row['CatID']) ? $row['CatID'] : NULL;

        $this->_['CatName'] = isset($row['CatName']) ? ucwords($row['CatName']) : NULL;
        $html = $this->view->view_display('index');
        $arr_block_html = $this->view->view_get_block_html($html);

        $total = $this->mdp_product_total_list_search($key, $catid);
        if ($total > 0) {
            $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_);
        } else {
            $this->_['paging'] = '';
        }
        $this->_['cur'] = $current;
        $rows = $this->mdp_product_list_search($key, $catid, $current, $limit);


        if (!empty($data_sel_box))
            foreach ($data_sel_box as $key => $value) {
                $sel_html .= $this->view->view_assign($arr_block_html['submenu'], array(
                    'sel_select' => $catid > 0 && $value['ID'] == $catid ? 'selected="true"' : NULL,
                    'ID' => $value['ID'],
                    'CatName' => $value['CatName']
                        ));
            }
        $this->_['data_select'] = $sel_html;
        if (!empty($rows))
            foreach ($rows as $row) {
                $res_html .= $this->view->view_assign($arr_block_html['product'], $row);
            }

        $shtml = $this->view->view_assign_block($html, array('htmlproduct' => $res_html));

        return $shtml;
    }

    public function add_action() {
        /* Check permission */
        if (!$this->per_check_permission('add', true)) {
            $this->_layout = 'error';
            $strhtml = $this->_['not_permission'];
        } else {
            $this->_layout = 'iframe';

            $this->_['sms'] = '';
            $this->_['dis_none_but'] = 'display:none;';

            /* LAY DANH SACH NGON NGU */
            $arr_row_titles = $this->mdl_lang_list(); /* lang max */
            $alang = array(); /* lang data change not empty */
            $arr_lang_insert = array(); /* lang data ok insert */

            /* Kiểm tra lang có dữ liệu */
            if (!empty($_POST['Name']) && !empty($arr_row_titles)) {
                var_dump($_POST);
                foreach ($arr_row_titles as $arr_row) {
                    $langid = $arr_row['lang_id'];

                    if (!(
                            empty($_POST['Name'][$langid]) &&
                            empty($_POST['FullContents'][$langid]) &&
                            empty($_POST['AltPic'][$langid]) &&
                            empty($_POST['SeoKey'][$langid]) &&
                            empty($_POST['Title'][$langid]) &&
                            empty($_POST['Desc'][$langid]) &&
                            empty($_POST['Keyword'][$langid]) &&
                            empty($_POST['TitleH1'][$langid]) &&
                            empty($_POST['TitleH2'][$langid]) &&
                            empty($_POST['DescF'][$langid])
                            )) {
                        $alang[] = $langid;
                    }
                }
            }

            /* Kiểm tra lỗi lang có dữ liệu */
            if (!empty($_POST['CID']) && !empty($_POST['Name']) && !empty($_POST['SeoKey'])) {
                if (!empty($alang)) {
                    foreach ($alang as $lang_id) {
                        $_POST['SeoKey'][$lang_id] = $this->str_seokey($_POST['SeoKey'][$lang_id]);
                        $seo_key = $this->mdp_get_seo_key($_POST['SeoKey'][$lang_id], $lang_id);
                        if ($seo_key > 0) {
                            $this->_['sms'] .= $this->_['check_seo_key'] . ' ' . $arr_row['lang_name'] . '<br />';
                        } else if (empty($_POST['Name'][$lang_id])) {
                            $this->_['sms'] .= $this->_['empty_prot_name'] . ' ' . $arr_row['lang_name'] . '<br />';
                        } else if (empty($_POST['SeoKey'][$lang_id])) {
                            $this->_['sms'] .= $this->_['empty_seo_key'] . ' ' . $arr_row['lang_name'] . '<br />';
                        } else if (empty($_POST['FullContents'][$lang_id])) {
                            $this->_['sms'] .= $this->_['empty_full_contents'] . ' ' . $arr_row['lang_name'] . '<br />';
                        } else {

                            /* Tabs */
                            $itabs = 1;
                            if (!empty($_POST['tabs'])) {
                                foreach ($_POST['tabs'][$lang_id] as $k => $v) {
                                    if (!empty($_POST['tabs'][$lang_id][$k]['title'])) {
                                        $_POST['_tabs'][$lang_id][$itabs]['title'] = urlencode($_POST['tabs'][$lang_id][$k]['title']);
                                        $_POST['_tabs'][$lang_id][$itabs]['content'] = urlencode($_POST['tabs'][$lang_id][$k]['content']);

                                        $_POST['js_tabs'][$lang_id] = json_encode($_POST['_tabs'][$lang_id]);



                                        $itabs++;
                                    }
                                }
                            }

                            /* Upload pic */
                            if (isset($_FILES['Img']) && !empty($_FILES['Img']['name'])) {
                                $img = $this->upload_img($_FILES['Img'], PATH_BASE . 'upload/images/products/', $seo_key);
                                if ($img) {
                                    $_POST['Img'] = $img;
                                } else {
                                    $this->_['sms'] = $this->_['img_not_format'];
                                }
                            }
                            $arr_lang_insert[] = $lang_id;
                        }
                    }
                }
            }


            /* Lưu những lang đã kiểm tra */
            if (!empty($arr_lang_insert)) {
                $add = 0;
                $id = 0;
                $done_ok = true;
                foreach ($arr_lang_insert as $lagid) {

                    if ($add == 0) {
                        $id = $this->mdp_prot_insert();
                        $add++;
                        if (!$id) {
                            $done_ok = false;
                        }
                    }

                    if ($id != 0 && $done_ok == true) {
                        $done = $this->mdp_prot_insert_lang($id, $lagid);
                        if ($done == false) {
                            $done_ok = false;
                        }
                    }
                }
                /* Điều hướng khi tất cả đều thành công */
                if ($done_ok == true) {
                    $this->request->rq_goto_window_parent(URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1] . '/' . (isset($this->_seokey[3]) && intval($this->_seokey[3]) < 1 ? $this->_seokey[3] . '/' : NULL));
                }
            }

            $this->_['title_editor'] = $this->_['title_editor_add'];
            $this->_['file_Img'] = URL_BASE . 'upload/noimg.png';
            $this->_['val_order'] = (isset($_POST['Order']) ? $_POST['Order'] : 1);
            $this->_['val_post_date'] = isset($_POST['DatePost']) ? $_POST['DatePost'] : date("d-m-Y H:i:s", time());
            $this->_['checked_index_follow'] = isset($_POST['NoIndexFollow']) && $_POST['NoIndexFollow'] == 1 ? 'checked="true"' : NULL;
            $this->_['checked_product_hot'] = isset($_POST['ProductHot']) && $_POST['ProductHot'] == 1 ? 'checked="true"' : NULL;
            $this->_['checked_active_post_date'] = isset($_POST['ActiveDatePost']) && $_POST['ActiveDatePost'] == 1 ? 'checked="true"' : NULL;
            $this->_['val_manufacturer'] = (isset($_POST['Manufacturer']) ? $_POST['Manufacturer'] : NULL);
            $this->_['val_code'] = (isset($_POST['Code']) ? $_POST['Code'] : 0);
            $this->_['val_size'] = (isset($_POST['Size']) ? $_POST['Size'] : 0);
            $this->_['val_price'] = (isset($_POST['Price']) ? $_POST['Price'] : 0);
            $this->_['val_discount'] = (isset($_POST['Discount']) ? $_POST['Discount'] : 0);
            $this->_['val_percent'] = (isset($_POST['Percent']) ? $_POST['Percent'] : 0);
            $this->_['val_fromDate'] = (isset($_POST['FromDate']) ? $_POST['FromDate'] : 0);
            $this->_['val_toDate'] = (isset($_POST['ToDate']) ? $_POST['ToDate'] : 0);
            $this->_['checked_product_secial'] = isset($_POST['ProductSpecial']) && $_POST['ProductSpecial'] == 1 ? 'checked="true"' : NULL;
            $this->_['checked_product_highlight'] = isset($_POST['ProductHighlight']) && $_POST['ProductHighlight'] == 1 ? 'checked="true"' : NULL;

            if (!empty($arr_row_titles)) {
                foreach ($arr_row_titles as $arr_row) {
                    $lang_id = $arr_row['lang_id'];

                    $this->_['tabs_title_' . $lang_id . '_1'] = $this->_['tabs_title_1'];
                    $this->_['tabs_title_' . $lang_id . '_2'] = $this->_['tabs_title_2'];
                    $this->_['tabs_title_' . $lang_id . '_3'] = $this->_['tabs_title_3'];
                    $this->_['tabs_content_' . $lang_id . '_1'] = '';
                    $this->_['tabs_content_' . $lang_id . '_2'] = '';
                    $this->_['tabs_content_' . $lang_id . '_3'] = '';

                    $this->_['Name_' . $lang_id] = isset($_POST['Name'][$lang_id]) ? $_POST['Name'][$lang_id] : NULL;
                    $this->_['SeoKey_' . $lang_id] = isset($_POST['SeoKey'][$lang_id]) ? $_POST['SeoKey'][$lang_id] : NULL;
                    $this->_['xTitle_' . $lang_id] = isset($_POST['Title'][$lang_id]) ? $_POST['Title'][$lang_id] : NULL;
                    $this->_['Desc_' . $lang_id] = isset($_POST['Desc'][$lang_id]) ? $_POST['Desc'][$lang_id] : NULL;
                    $this->_['Keyword_' . $lang_id] = isset($_POST['Keyword'][$lang_id]) ? $_POST['Keyword'][$lang_id] : NULL;
                    $this->_['TitleH1_' . $lang_id] = isset($_POST['TitleH1'][$lang_id]) ? $_POST['TitleH1'][$lang_id] : NULL;
                    $this->_['TitleH2_' . $lang_id] = isset($_POST['TitleH2'][$lang_id]) ? $_POST['TitleH2'][$lang_id] : NULL;
                    $this->_['DescF_' . $lang_id] = isset($_POST['DescF'][$lang_id]) ? $_POST['DescF'][$lang_id] : NULL;
                    $this->_['AltPic_' . $lang_id] = isset($_POST['AltPic'][$lang_id]) ? $_POST['AltPic'][$lang_id] : NULL;
                    $this->_['FullContents_' . $lang_id] = isset($_POST['FullContents'][$lang_id]) ? $_POST['FullContents'][$lang_id] : NULL;

                    $this->_['selected_show_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'show' ? 'selected="true"' : NULL;
                    $this->_['selected_hide_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'hide' ? 'selected="true"' : NULL;
                    $this->_['selected_del_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'del' ? 'selected="true"' : NULL;
                    $this->_['checked_locks_' . $lang_id] = isset($_POST['Locks'][$lang_id]) && $_POST['Locks'][$lang_id] == '1' ? 'checked="true"' : NULL;

                    /* create tabs */
                    $this->_['total_tab_' . $lang_id] = 4;
                    $this->_['js_ckedit_' . $lang_id] = Null;
                    $this->_['tabs_tabs_li_' . $lang_id] = Null;
                    $this->_['tabs_tabs_div_' . $lang_id] = Null;
                }
            }


            $noid = isset($this->_seokey[2]) ? intval($this->_seokey[2]) : 0;
            $resultHtml = '';
            $resultHtmlx = '';
            $resultTile = '';

            $html = $this->view->view_display("editor");
            /* Name Block HTML */
            $this->arrBlockEditor = $this->view->view_get_block_html($html);
            $tabs_title = isset($this->arrBlockEditor['tabs_title']) ? $this->arrBlockEditor['tabs_title'] : Null;
            $tabs_title_ul = isset($this->arrBlockEditor['tabs_title_ul']) ? $this->arrBlockEditor['tabs_title_ul'] : Null;
            $tabs_contents = isset($this->arrBlockEditor['tabs_contents']) ? $this->arrBlockEditor['tabs_contents'] : Null;
            $select_option_com = isset($this->arrBlockEditor['select_option']) ? $this->arrBlockEditor['select_option'] : Null;
            /* set data to select box location */
            $select_location = isset($this->arrBlockEditor['list_location']) ? $this->arrBlockEditor['list_location'] : Null;

            $str = '';
            $arr_listlocation = $this->mdp_list_location();
            if (!empty($arr_listlocation)) {
                foreach ($arr_listlocation as $key => $value) {
                    $str .= $this->view->view_assign($select_location, array(
                        'selected' => $value['Defaults'] == 'true' ? 'selected="true"' : NULL,
                        'location_id' => $value['ID'],
                        'location_name' => $value['LocationName']
                            ));
                }
                $this->_['location_html'] = $str;
            }
            /* end set data to select box location */

            /* SELECT BOX CAT */
            $seo3 = isset($this->_seokey[3]) ? $this->_seokey[3] : null;
            $row = $this->mdc_get_cat_cid($seo3);
            $CID = isset($row['CatID']) ? $row['CatID'] : 0;
            $CID = isset($_POST['CID']) && !empty($_POST['CID']) ? $_POST['CID'] : $CID;
            $this->_['option_cat'] = $this->cat_list_box(2, 0, $noid, $CID);


            /* TAO TAB TITLE */
            if (!empty($arr_row_titles)) {
                foreach ($arr_row_titles as $arr_row_title) {
                    $resultTile .= $this->view->view_assign($tabs_title, $arr_row_title);
                }
            }
            $resultHtml .= $this->view->view_assign($tabs_title_ul, array('tabs_li' => $resultTile));
            /* TAO NOI DUNG TAB */
            if (!empty($arr_row_titles)) {
                foreach ($arr_row_titles as $arr_row_title) {
                    $resultHtmlx .= $this->view->view_assign($tabs_contents, $arr_row_title);
                }
            }
            $resultHtml .= $resultHtmlx;


            /* CHEN MANG NGON NGU DE TAO HTML */
            $result = $this->view->view_assign($resultHtml, $this->_);
            $strhtml = $this->view->view_assign_block($html, array('htmlCat' => $result));
        }
        return $strhtml;
    }

    public function edit_action() {

        if (!$this->per_check_permission('edit', true)) {
            $this->_layout = 'error';
            $strhtml = $this->_['not_permission'];
        } else {
            $this->_layout = 'iframe';

            $this->_['sms'] = '';
            $this->_['dis_none'] = '';
            $html = $this->view->view_display("editor");
            $this->arrBlockEditor = $this->view->view_get_block_html($html);

            $artid = isset($this->_seokey) ? intval(end($this->_seokey)) : null;
            if ($artid) {
                /* Database */
                $prot_rows = $this->mdp_get_art($artid);

                /* LAY DANH SACH NGON NGU */
                $arr_row_titles = $this->mdl_lang_list(); /* lang max */
                $alang = array(); /* lang data change not empty */
                $arr_lang_insert = array(); /* lang data ok insert */

                /* Kiểm tra lang có dữ liệu */
                if (!empty($_POST['Name']) && !empty($arr_row_titles)) {
                    foreach ($arr_row_titles as $arr_row) {
                        $langid = $arr_row['lang_id'];

                        if (!(
                                empty($_POST['Name'][$langid]) &&
                                empty($_POST['FullContents'][$langid]) &&
                                empty($_POST['AltPic'][$langid]) &&
                                empty($_POST['SeoKey'][$langid]) &&
                                empty($_POST['Title'][$langid]) &&
                                empty($_POST['Desc'][$langid]) &&
                                empty($_POST['Keyword'][$langid]) &&
                                empty($_POST['TitleH1'][$langid]) &&
                                empty($_POST['TitleH2'][$langid]) &&
                                empty($_POST['DescF'][$langid])
                                )) {
                            $alang[] = $langid;
                        }
                    }
                }

                /* Kiểm tra lỗi lang có dữ liệu */
                if (!empty($_POST['CID']) && !empty($_POST['Name']) && !empty($_POST['SeoKey'])) {
                    if (!empty($alang)) {
                        foreach ($alang as $lang_id) {
                            $_POST['SeoKey'][$lang_id] = $this->str_seokey($_POST['SeoKey'][$lang_id]);
                            $seo_key = $this->mdp_get_seo_key($_POST['SeoKey'][$lang_id], $lang_id, $prot_rows['ID']);
                            if ($seo_key > 0) {
                                $this->_['sms'] .= $this->_['check_seo_key'] . ' ' . $arr_row['lang_name'] . '<br />';
                            } else if (empty($_POST['Name'][$lang_id])) {
                                $this->_['sms'] .= $this->_['empty_prot_name'] . ' ' . $arr_row['lang_name'] . '<br />';
                            } else if (empty($_POST['SeoKey'][$lang_id])) {
                                $this->_['sms'] .= $this->_['empty_seo_key'] . ' ' . $arr_row['lang_name'] . '<br />';
                            } else if (empty($_POST['FullContents'][$lang_id])) {
                                $this->_['sms'] .= $this->_['empty_full_contents'] . ' ' . $arr_row['lang_name'] . '<br />';
                            } else {

                                /* Tabs */
                                $itabs = 1;

                                if (!empty($_POST['tabs'])) {
                                    foreach ($_POST['tabs'][$lang_id] as $k => $v) {
                                        if (!empty($_POST['tabs'][$lang_id][$k]['title'])) {
                                            $_POST['_tabs'][$lang_id][$itabs]['title'] = urlencode($_POST['tabs'][$lang_id][$k]['title']);
                                            $_POST['_tabs'][$lang_id][$itabs]['content'] = urlencode($_POST['tabs'][$lang_id][$k]['content']);

                                            $_POST['js_tabs'][$lang_id] = json_encode($_POST['_tabs'][$lang_id]);

                                            $itabs++;
                                        }
                                    }
                                }

                                /* Upload pic */
                                if (isset($_FILES['Img']) && !empty($_FILES['Img']['name'])) {
                                    $img = $this->upload_img($_FILES['Img'], PATH_BASE . 'upload/images/products/', $seo_key);
                                    if ($img) {
                                        $_POST['Img'] = $img;
                                    } else {
                                        $this->_['sms'] = $this->_['img_not_format'];
                                    }
                                }
                                $arr_lang_insert[] = $lang_id;
                            }
                        }
                    }
                }

                /* Lưu những lang đã kiểm tra */
                if (!empty($arr_lang_insert)) {
                    $add = 0;
                    $id = 0;
                    $done_ok = true;
                    foreach ($arr_lang_insert as $lagid) {
                        if ($add == 0) {
                            $rs = $this->mdp_prot_update($prot_rows['ID']);
                            $add++;
                            if (!$rs) {
                                $done_ok = false;
                            }
                        }

                        if ($rs && $done_ok == true) {
                            $done = $this->mdp_prot_update_lang($prot_rows['ID'], $lagid);
                            if (!$done) {
                                $done_ok = false;
                            }
                        }
                    }
                    /* Điều hướng khi tất cả đều thành công */
                    if ($done_ok == true) {
                        $this->request->rq_goto_window_parent(URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1] . '/' . (isset($this->_seokey[3]) ? $this->_seokey[3] . '/' : NULL));
                    }
                }

                if (!empty($prot_rows['Picture'])) {
                    $this->_['path_file_Img'] = urlencode($prot_rows['Picture']);
                    $this->_['file_Img'] = URL_BASE . 'upload/images/products/' . $prot_rows['Picture'];
                    $this->_['artID'] = $prot_rows['ID'];
                    $this->_['dis_none_input'] = 'display:none;';
                } else {

                    $this->_['file_Img'] = URL_BASE . 'upload/noimg.png';
                    $this->_['dis_none_but'] = 'display:none;';
                }

                /* Database set from */
                $this->_['title_editor'] = $this->_['title_editor_edit'];
                $this->_['val_order'] = (isset($_POST['Order']) ? $_POST['Order'] : $prot_rows['Order']);
                $this->_['val_post_date'] = isset($_POST['DatePost']) ? $_POST['DatePost'] : (isset($prot_rows['DatePost']) && !empty($prot_rows['DatePost']) ? date("d-m-Y H:i:s", $prot_rows['DatePost']) : date("d-m-Y H:i:s", time()));
                $this->_['checked_index_follow'] = isset($_POST['NoIndexFollow']) && $_POST['NoIndexFollow'] == 1 ? 'checked="true"' : ($prot_rows['NoIndexFollow'] ? 'checked="true"' : NULL);
                $this->_['checked_product_hot'] = isset($_POST['ProductHot']) && $_POST['ProductHot'] == 1 ? 'checked="true"' : ($prot_rows['ProductHot'] ? 'checked="true"' : NULL);
                $this->_['checked_active_post_date'] = isset($_POST['ActiveDatePost']) && $_POST['ActiveDatePost'] == 1 ? 'checked="true"' : ($prot_rows['ActiveDatePost'] ? 'checked="true"' : NULL);
                $this->_['val_manufacturer'] = (isset($_POST['Manufacturer']) ? $_POST['Manufacturer'] : $prot_rows['Order']);
                $this->_['val_code'] = (isset($_POST['Code']) ? $_POST['Code'] : $prot_rows['Code']);
                $this->_['val_size'] = (isset($_POST['Size']) ? $_POST['Size'] : $prot_rows['Size']);
                $this->_['val_price'] = (isset($_POST['Price']) ? $_POST['Price'] : $prot_rows['Price']);
                $this->_['val_discount'] = (isset($_POST['Discount']) ? $_POST['Discount'] : $prot_rows['Discount']);
                $this->_['val_percent'] = (isset($_POST['Percent']) ? $_POST['Percent'] : $prot_rows['Percent']);
                $this->_['val_fromDate'] = (isset($_POST['FromDate']) ? $_POST['FromDate'] : ($prot_rows['FromDate'] != 0 && !empty($prot_rows['FromDate']) ? date("d-m-Y", $prot_rows['FromDate']) : 0));
                $this->_['val_toDate'] = (isset($_POST['ToDate']) ? $_POST['ToDate'] : ($prot_rows['ToDate'] != 0 && !empty($prot_rows['ToDate']) ? date("d-m-Y", $prot_rows['ToDate']) : 0));
                $this->_['checked_product_secial'] = isset($_POST['ProductSpecial']) && $_POST['ProductSpecial'] == 1 ? 'checked="true"' : ($prot_rows['ProductSpecial'] ? 'checked="true"' : NULL);
                $this->_['checked_product_highlight'] = isset($_POST['ProductHighlight']) && $_POST['ProductHighlight'] == 1 ? 'checked="true"' : ($prot_rows['ProductHighlight'] ? 'checked="true"' : NULL);

                /* Database set from lang */
                if (!empty($arr_row_titles)) {
                    foreach ($arr_row_titles as $arr_row) {
                        $lang_id = $arr_row['lang_id'];
                        $prot_lang_row = $this->mdp_get_prot_lang($prot_rows['ID'], $lang_id);
                        $this->_['Name_' . $lang_id] = isset($_POST['Name'][$lang_id]) ? $_POST['Name'][$lang_id] : $prot_lang_row['Name'];
                        $this->_['SeoKey_' . $lang_id] = isset($_POST['SeoKey'][$lang_id]) ? $_POST['SeoKey'][$lang_id] : $prot_lang_row['SeoKey'];
                        $this->_['xTitle_' . $lang_id] = isset($_POST['Title'][$lang_id]) ? $_POST['Title'][$lang_id] : $prot_lang_row['Title'];
                        $this->_['Desc_' . $lang_id] = isset($_POST['Desc'][$lang_id]) ? $_POST['Desc'][$lang_id] : $prot_lang_row['Desc'];
                        $this->_['Keyword_' . $lang_id] = isset($_POST['Keyword'][$lang_id]) ? $_POST['Keyword'][$lang_id] : $prot_lang_row['Keyword'];
                        $this->_['TitleH1_' . $lang_id] = isset($_POST['TitleH1'][$lang_id]) ? $_POST['TitleH1'][$lang_id] : $prot_lang_row['TitleH1'];
                        $this->_['TitleH2_' . $lang_id] = isset($_POST['TitleH2'][$lang_id]) ? $_POST['TitleH2'][$lang_id] : $prot_lang_row['TitleH2'];
                        $this->_['DescF_' . $lang_id] = isset($_POST['DescF'][$lang_id]) ? $_POST['DescF'][$lang_id] : $prot_lang_row['DescF'];
                        $this->_['AltPic_' . $lang_id] = isset($_POST['AltPic'][$lang_id]) ? $_POST['AltPic'][$lang_id] : $prot_lang_row['AltPic'];
                        $this->_['FullContents_' . $lang_id] = isset($_POST['FullContents'][$lang_id]) ? $_POST['FullContents'][$lang_id] : $prot_lang_row['FullContents'];

                        $this->_['selected_show_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'show' ? 'selected="true"' : ($prot_lang_row['Status'] == 'show' ? 'selected="true"' : NULL);
                        $this->_['selected_hide_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'hide' ? 'selected="true"' : ($prot_lang_row['Status'] == 'hide' ? 'selected="true"' : NULL);
                        $this->_['selected_del_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'del' ? 'selected="true"' : ($prot_lang_row['Status'] == 'del' ? 'selected="true"' : NULL);
                        $this->_['checked_locks_' . $lang_id] = isset($_POST['Locks'][$lang_id]) && $_POST['Locks'][$lang_id] == '1' ? 'checked="true"' : ($prot_lang_row['Locks'] == 1 ? 'checked="true"' : NULL);

                        /* create tabs */
                        $json[$lang_id] = json_decode($prot_lang_row['Tabs']);
                        $j = 1;
                        $this->_['tabs_tabs_li_' . $lang_id] = '';
                        $this->_['tabs_tabs_div_' . $lang_id] = '';
                        if (!empty($json[$lang_id])) {
                            /* Content not empty */
                            foreach ($json[$lang_id] as $t) {
                                if ($j > 3) {

                                    $this->_['tabs_tabs_li_' . $lang_id] .= $this->view->view_assign(
                                            (isset($this->arrBlockEditor['tabs_tabs_title']) ? $this->arrBlockEditor['tabs_tabs_title'] : Null), array(
                                        'itemtab' => $j,
                                        'lang_id' => $lang_id
                                            )
                                    );
                                    $this->_['tabs_tabs_div_' . $lang_id] .= $this->view->view_assign(
                                            (isset($this->arrBlockEditor['tabs_tabs_content']) ? $this->arrBlockEditor['tabs_tabs_content'] : Null), array(
                                        'itemtab' => $j,
                                        'lang_id' => $lang_id
                                            )
                                    );
                                }

                                $this->_['tabs_title_' . $lang_id . '_' . $j] = urldecode($t->title);
                                $this->_['tabs_content_' . $lang_id . '_' . $j] = urldecode($t->content);

                                $j++;
                            }
                        } else {
                            /* Content is empty */
                            $this->_['tabs_title_' . $lang_id . '_1'] = $this->_['tabs_title_1'];
                            $this->_['tabs_title_' . $lang_id . '_2'] = $this->_['tabs_title_2'];
                            $this->_['tabs_title_' . $lang_id . '_3'] = $this->_['tabs_title_3'];
                            $this->_['tabs_content_' . $lang_id . '_1'] = '';
                            $this->_['tabs_content_' . $lang_id . '_2'] = '';
                            $this->_['tabs_content_' . $lang_id . '_3'] = '';
                        }
                        $this->_['total_tab_' . $lang_id] = $j;
                    }
                }

                $resultHtml = '';
                $resultHtmlx = '';
                $resultTile = '';

                /* Name Block HTML */
                $tabs_title = isset($this->arrBlockEditor['tabs_title']) ? $this->arrBlockEditor['tabs_title'] : Null;
                $tabs_title_ul = isset($this->arrBlockEditor['tabs_title_ul']) ? $this->arrBlockEditor['tabs_title_ul'] : Null;
                $tabs_contents = isset($this->arrBlockEditor['tabs_contents']) ? $this->arrBlockEditor['tabs_contents'] : Null;
                $select_option_com = isset($this->arrBlockEditor['select_option']) ? $this->arrBlockEditor['select_option'] : Null;
                /* set data to select box location */
                $select_location = isset($this->arrBlockEditor['list_location']) ? $this->arrBlockEditor['list_location'] : Null;

                $str = '';

                $arr_listlocation = $this->mdp_list_location();
                if (!empty($arr_listlocation)) {
                    foreach ($arr_listlocation as $key => $value) {
                        $str .= $this->view->view_assign($select_location, array(
                            'selected' => $value['ID'] == $prot_rows['LocationID'] ? 'selected="true"' : NULL,
                            'location_id' => $value['ID'],
                            'location_name' => $value['LocationName']
                                ));
                    }
                    $this->_['location_html'] = $str;
                }
                /* end set data to select box location */
                /* SELECT BOX CAT */
                $noid = isset($this->_seokey[2]) ? intval($this->_seokey[2]) : 0;
                $CID = isset($_POST['CID']) && !empty($_POST['CID']) ? $_POST['CID'] : $prot_rows['CID'];
                $this->_['option_cat'] = $this->cat_list_box(2, 0, $noid, $CID);


                /* TAO TAB TITLE */
                if (!empty($arr_row_titles)) {
                    foreach ($arr_row_titles as $arr_row_title) {
                        $resultTile .= $this->view->view_assign($tabs_title, $arr_row_title);
                    }
                }
                $resultHtml .= $this->view->view_assign($tabs_title_ul, array('tabs_li' => $resultTile));
                /* TAO NOI DUNG TAB */
                if (!empty($arr_row_titles)) {
                    foreach ($arr_row_titles as $arr_row_title) {
                        $resultHtmlx .= $this->view->view_assign($tabs_contents, $arr_row_title);
                    }
                }
                $resultHtml .= $resultHtmlx;

                /* CHEN MANG NGON NGU DE TAO HTML */
                $result = $this->view->view_assign($resultHtml, $this->_);
                $strhtml = $this->view->view_assign_block($html, array('htmlCat' => $result));
            }
        }
        return $strhtml;
    }

    private function cat_list_box($id, $indent, $noid = 0, $select) {
        $indent +=1;
        $str = '';
        $rr = $this->mdc_cat_list_box($id, $noid, 'product');

        if (!empty($rr)) {
            foreach ($rr as $key => $row) {
                $arr_row = array(
                    'selected' => ($select == $row['ID'] ? 'selected="true"' : NULL),
                    'com_value' => $row['ID'],
                    'com_text' => str_repeat("&nbsp;&nbsp;", $indent) . $row['CatName']
                );

                $str .= $this->view->view_assign($this->arrBlockEditor['select_option'], $arr_row);

                if (isset($row['ID']) && $str) {
                    $str .= $this->cat_list_box($row['ID'], $indent, $noid, $select);
                }
            }
        }

        return $str;
    }

    /* Add vao com act */

    public function show_action() {
        
    }

    public function hide_action() {
        
    }

    public function del_action() {
        
    }

    public function delete_action() {
        
    }

    /* End Add vao com act */

    public function index_ajax_action() {
        $this->_layout = 'NO_LAYOUT';

        switch ($this->_seokey[3]) {
            case "search":
                return $this->index_action($this->_seokey[4], $this->_seokey[5]);
                break;
            default:
                $active = isset($this->_seokey[3]) ? $this->_seokey[3] : null;
                $str_id = isset($this->_seokey[4]) ? $this->_seokey[4] : null;
                $str_path = isset($this->_seokey[5]) ? $this->_seokey[5] : null;
                if ($this->per_check_permission($active, true)) {
                    if ($active == 'delpic' && !empty($str_path)) {

                        $this->mdp_prot_del_pic($str_id);
                        unlink(PATH_BASE . 'upload/images/products/' . $str_path);
                    }

                    $done = $this->mdp_tool_func($this->model, $active, $str_id);
                    if ($done) {
                        return 'ok';
                    } else {
                        return 'noOk';
                    }
                } else {
                    return 'noper';
                }
                break;
        }
    }

}
